public class 分割数组的最大值 {
    public int splitArray(int[] nums, int k) {
        //找出二分的最大范围[0,sum]
        int sum = 0;
        for(int num : nums){
            sum += num;
        }
        int l = 0;
        int r = sum;
        while(l < r){
            int mid = (l + r) >> 1;
            if(f(nums,mid) <= k){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        return l;
    }

    private int f(int[] nums, int mid) {
        int sum = 0;
        int part = 1;

        for(int i = 0;i < nums.length;i++){
            if(sum + nums[i]< mid){
                sum += nums[i];
            }else{
                sum = nums[i];
                part++;
            }
        }
        return part;
    }
}
